<?php

class Database{

	public function __construct(){
		//load db driver
		$this->getDatabaseDriver();
	}
	
	// High order methods
	public function get_row($table, $getByColum, $getByValue){
			return Reg::get('database')->get_row($table, $getByColum, $getByValue);// table, get by colum, get by value
	}
	
	public function  get_rows($table,$valuesToReturn,$start=0,$limit=50,$where=null){
			return Reg::get('database')->get_rows($table,$valuesToReturn,$start,$limit,$where);
	}
		
	//We has all possible feilds know to us since we got this all from db anyway, hence we know where everything goes
	public function set_row($table,$getByColum,$useValue,$keyed_value_array){
		return  Reg::get('database')->set_row($table,$getByColum,$useValue,$keyed_value_array);
	}
	//create_row('users',array('new','data','andsuch'))
	public function create_row($table,$values_array){
		//make user

		return Reg::get('database')->create_row($table,$values_array);
	}
	
	public function get_rowcount($table,$colum,$where=null){
			return Reg::get('database')->get_rowcount($table,$colum,$where);
	}
	public function remove_row($table,$colum,$removeOnValue){
			return Reg::get('database')->remove_row($table,$colum,$removeOnValue);
	}
		
	//Low order methods
	public function query($sql){
		return Reg::get('database')->query($sql);
	}
	
	public function fetchRow(){
		return Reg::get('database')->fetchRow();
	}
	
	public function countRows(){
		return Reg::get('database')->countRows();
	}
	
	public function cleanString($str){
		return Reg::get('database')->cleanString($str);
	}
	
	/*
	CreateDatabase 
	Configure and load specifed database Object
	*/
	private function getDatabaseDriver(){

		if(Reg::get('database')!==null) return;
		//Db driver not loaded, create new one

			if(Reg::get('database_driver')=='flat'){
				//Flat file
				die(" Flat file is disabled in this version. Please use MySql");
				$dbOpt['directory'] = Reg::get('data_path');
				$dbOpt['ext'] 		= Reg::get('ext');
				$db_driver = new FlatFileDB($dbOpt);
			}else{
				//SQL
				$dbOpt['sql_dbname'] 	= Reg::get('sql_dbname');
				$dbOpt['sql_username'] 	= Reg::get('sql_username');
				$dbOpt['sql_pass'] 		= Reg::get('sql_pass');
				$dbOpt['sql_host'] 		= Reg::get('sql_host');
				$dbOpt['sql_prefix'] 	= Reg::get('sql_prefix');
				$dbOpt['safe_mode'] 	= Reg::get('safe_mode');
				$db_driver = new SqlDB($dbOpt);
			}
			
			Reg::set('database',$db_driver);
	}







}